/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.haipv.gtkhoa501.screen;

import com.haipv.gtkhoa501.controller.Event;
import com.haipv.gtkhoa501.controller.StorageData;
import com.haipv.gtkhoa501.model.AnswerCode;
import com.haipv.gtkhoa501.model.Question;
import com.haipv.gtkhoa501.model.Var;
import com.haipv.gtkhoa501.util.Util;
import com.nokia.lwuit.components.HeaderBar;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.html.HTMLComponent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import java.io.IOException;

/**
 *
 * @author PHANHAI
 * Giao diện hiển thị câu ôn tập
 */
public class ReviewScreen extends BaseScreen implements ActionListener {

    String colorFirst = "<span style=\"color:#49A530\">";
    String colorLast = "</span>";
    String xuongDong = "</br></br>";
    int index = 1;
    Command back = new Command(Var.back);
    Command search = new Command(Var.search);

    public ReviewScreen(int index) {
        if (controller.categoryBarReview.getVisibility() == false) {
            controller.categoryBarReview.setVisibility(true);
        }
        this.index = index;
    }

    public void showForm() {
        Question question = StorageData.getInstance().getQuestion(index);

        Form form = new Form();
        form.setScrollableY(false);

        HeaderBar header = null;
        try {
            header = new HeaderBar(Var.cau + index);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        header.setHeaderTitleColor(Var.colorHearder);
        header.getStyle().setBgColor(Var.colorWhite);
        header.setScrollable(false);

        form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        form.addComponent(header);
//form.setTitle(Var.cau + index);
        Container con = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        Container con1 = new Container();
        Container con2 = new Container(new BorderLayout());

        HTMLComponent htmlComp = new HTMLComponent(null);
        htmlComp.setBodyText("   " + question.getContent() + xuongDong, "UTF-8");
        con1.addComponent(htmlComp);
        con1.getStyle().setMargin(5, 15, 5, 5);
        con.addComponent(con1);


        //System.out.println("question.getImg():  "+question.getImg());
        if (question.getImg().length() != 0) {
            try {
                int so = Integer.parseInt(question.getImg());
                Label lb = null;
                lb = new Label(Image.createImage("/images/" + so + ".jpg"));
                lb.getStyle().setAlignment(Label.CENTER);
                //lb.getStyle().setMargin(10, 10, 5, 5);
                con2.getStyle().setAlignment(Container.CENTER);
                con2.addComponent(BorderLayout.CENTER, lb);

                con.addComponent(con2);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        HTMLComponent html[] = new HTMLComponent[question.getOptions().size()];
        Container contai[] = new Container[question.getOptions().size()];
        String answer = null;
        int code = question.getAnswer();
        int option;
        for (int i = 0; i < question.getOptions().size(); i++) {
            option = i + 1;
            html[i] = new HTMLComponent(null);
            contai[i] = new Container();
            //  doan nay anh cho doan kiem tra neu la ansewe tra loi thi dừng chú thích cho em 
            answer = question.getOptions().elementAt(i).toString();
            if (AnswerCode.checkAnswer(code, option)) {
                // Dung
                answer = colorFirst + answer + colorLast;
            }

            html[i].setBodyText(" " + answer, "UTF-8");

            contai[i].addComponent(html[i]);

            contai[i].getStyle().setMargin(0, 0, 5, 5);
            con.addComponent(contai[i]);
        }
        con.setScrollableY(true);
        form.addComponent(con);
        form.setBackCommand(back);
        form.addCommand(back);
        form.setDefaultCommand(search);
        form.addCommand(search);
        form.addCommandListener(this);
        form.show();
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getCommand() == back) {
            controller.handleEvent(Event.MAIN_ACTION, null);
        } else if (ae.getCommand() == search) {
            controller.handleEvent(Event.REVIEW_SEARCH, null);
        }
    }
}
